package com.huaxia.day19;

import com.huaxia.day11.Student;

import java.util.Optional;

public class OptionalTest {
    public static void main(String[] args) {
        equalA("a");
        Student student = new Student();
        System.out.println(nameLegth1(student));
        System.out.println(nameLegth2(student));
        test2();

    }

    static boolean equalA(String str) {
        boolean res = false;
        if (str != null) {
            return str.equals("A");
        }

        return res;
    }

    static int nameLegth1(Student student){
        if (student== null || student.getName() == null){
            return 0;
        }
        return student.getName().length();
    }

    static int nameLegth2(Student student){

        return Optional.ofNullable(student)
                .map(Student::getName)
                .map(String::length)
                .orElse(0);
    }

    static void  test2(){
        Student student = null;
        Optional optional = Optional.ofNullable(student);
        if (optional.isPresent()){
            System.out.println("非空");
        }else {
            System.out.println("为空");
        }
    }
}
